home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_25 / example2.c < prev    next >
C/C++ Source or Header  |  1995-01-01  |  5KB  |  164 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*                 Digitized Voice Programmer's Toolkit                 */
  4. /*                 ------------------------------------                 */
  5. /*                            version 3.0                               */
  6. /*                                                                      */
  7. /*              Copyright (c) 1988-1993, Farpoint Software              */
  8. /*                                                                      */
  9. /*                                                                      */
  10. /*          Example of how to use FSSPEAK.EXE as a TSR program          */
  11. /*                                                                      */
  12. /************************************************************************/
  13.  
  14. /****** This program should be compiled using "Large" memory model. ******/
  15.  
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <stdio.h>
  19. #include <stddef.h>
  20. #include <malloc.h>
  21. #include <process.h>
  22. #include <dos.h>
  23. #include <share.h>
  24. #include <string.h>
  25.  
  26. /*-------------------------------------------------------------------*/
  27.  
  28. /* prototype for the FSSPEAK TSR entry point */
  29.  
  30. long (pascal __far *FSSPEAK_ENTRYPOINT)(int command, unsigned int parm2,
  31.                                unsigned char __far *parm3, long parm4);
  32.  
  33. /*-------------------------------------------------------------------*/
  34.  
  35. /* name of the data file to be played */
  36.  
  37. char datafilename[] = "DEMO.VOI";
  38.  
  39. /* sample rate of the data file */
  40.  
  41. unsigned int samples_per_sec = 16572;  // default for DVPT
  42.  
  43. /* compression algorithm to be used; 0 = none, 1 = uLaw, 2 = ADPCM */
  44.  
  45. int compression = 0;
  46.  
  47. /*-------------------------------------------------------------------*/
  48.  
  49. void main(int argc, char **argv)
  50.  
  51. {
  52. int destination;
  53. long returncode;
  54. int loadcode;
  55. char arg2[10];
  56. unsigned char __far *buffer;
  57. int handle;
  58. long size;
  59. unsigned isize;
  60.  
  61. /* get the destination parameter from the command line */
  62.  
  63. if ( argc < 2 )         // no parameters...
  64.     destination = -1;   // ... auto-locate
  65. else
  66.     {
  67.     argv++;                             // point to 1st parameter
  68.     if ( !stricmp(*argv, "LPT1") )
  69.         destination = 1;
  70.     else if ( !stricmp(*argv, "LPT2") )
  71.         destination = 2;
  72.     else if ( !stricmp(*argv, "LPT3") )
  73.         destination = 3;
  74.     else if ( !stricmp(*argv, "SPKR") )
  75.         destination = 0;               // use internal speaker
  76.     else
  77.         destination = -1;              // invoke auto-locate if parameter
  78.                                        //   is not interpretable
  79.     }
  80.  
  81. /* Step 1 (required only once): Load TSR into memory. */
  82.  
  83.   /* create command line argument 2 */
  84.  
  85. sprintf(arg2, "%4.4X:%4.4X",
  86.         (unsigned)(((long)(&FSSPEAK_ENTRYPOINT)) >> 16),
  87.         (unsigned)(((long)(&FSSPEAK_ENTRYPOINT)) & 0xFFFF) );
  88.  
  89.   /* invoke the executable */
  90.  
  91. loadcode = _spawnl(_P_WAIT, "FSSPEAK.EXE", "FSSPEAK.EXE",
  92.                      "/L", arg2, NULL);
  93.  
  94.   /* NOTE: Only the bottom 8 bits are used for TSR load spawn call! */
  95.  
  96. loadcode &= 0x00FF;
  97.  
  98.   /* test return code; see docs for complete explanation */
  99.  
  100. if ( loadcode != 0 )         // if not OK
  101.     {
  102.     printf("TSR load attempt returned %d.\n", loadcode);
  103.     exit(1);
  104.     }
  105.  
  106. /* Step 2: Set up the various operating parameters. */
  107.  
  108. printf("The following two calls will return -1 if FSSPEAK is the Standard Version.\n");
  109. printf("Set rate returned %ld\n",
  110.        FSSPEAK_ENTRYPOINT(1, samples_per_sec, NULL, 0L));
  111. printf("Set compression returned %ld\n",
  112.        FSSPEAK_ENTRYPOINT(2, compression, NULL, 0L));
  113.  
  114.     /* "set destination and calibrate" must be done last */
  115.  
  116. returncode = FSSPEAK_ENTRYPOINT(3, (unsigned int)destination, NULL, 0L);
  117.  
  118.     /* test return code; see docs for more detail */
  119.  
  120. if ( returncode != 0L )
  121.     {
  122.     printf("Calibration attempt returned %d.\n", (int)returncode);
  123.     _spawnl(_P_WAIT, "FSSPEAK.EXE", "FSSPEAK.EXE", "/U", NULL);  // unload TSR
  124.     exit(1);
  125.     }
  126.  
  127. /* Step 3: Play the data through the internal speaker or LPT port.
  128.            This can be done multiple times after a single calibration. */
  129.  
  130. buffer = _fmalloc(65504U);
  131. if ( buffer == NULL )
  132.     {
  133.     printf("Unable to allocate memory.\n");
  134.     _spawnl(_P_WAIT, "FSSPEAK.EXE", "FSSPEAK.EXE", "/U", NULL);  // unload TSR
  135.     exit(1);
  136.     }
  137.  
  138. /* >>>> NOTE: Skipping error checking to enhance code readability. <<<< */
  139.  
  140. printf("For simplicity, this program plays only the first 64k of the file.\n");
  141.  
  142. _dos_open(datafilename, _O_RDONLY|_SH_COMPAT, &handle);
  143. _dos_read(handle, buffer, 65504U, &isize);
  144. _dos_close(handle);
  145. size = (long)isize;
  146.  
  147. if ( size <= 0L )
  148.     {
  149.     _ffree(buffer);
  150.     printf("Data file read error.\n");
  151.     _spawnl(_P_WAIT, "FSSPEAK.EXE", "FSSPEAK.EXE", "/U", NULL);  // unload TSR
  152.     exit(1);
  153.     }
  154.  
  155. FSSPEAK_ENTRYPOINT(4, 0, buffer, size);
  156.  
  157. _ffree(buffer);
  158.  
  159. /* Step 4: Unload the TSR from memory. */
  160.  
  161. _spawnl(_P_WAIT, "FSSPEAK.EXE", "FSSPEAK.EXE", "/U", NULL);
  162.  
  163. }
  164.